// $Id: StatusBar.java 10 2008-09-16 08:13:42Z moyekj@yahoo.com $

package com.hme.tivo.videostream;

import com.tivo.hme.bananas.BView;
import com.tivo.hme.bananas.BText;

public class StatusBar {
   public BView main;
   public BView bar;
   public BText textDUR;
   public BText textPOS;
   public BView textPOSBG;
   public Boolean visible = false;
   
   public StatusBar(BView parent) {
      debug.print("parent=" + parent);
      // Background view
      main = new BView(
         parent, GLOBAL.statusBG_X, GLOBAL.statusBG_Y,
         GLOBAL.statusBG_W+53, GLOBAL.statusBG_H
      );	  
      main.setResource(GLOBAL.statusBG_COLOR);
      main.setTransparency((float)0.25);

      // Bar
      bar = new BView(
         parent, GLOBAL.statusBG_X, GLOBAL.statusBG_Y,
         GLOBAL.statusBG_W, GLOBAL.statusBG_H
      );
      bar.setResource(GLOBAL.statusBAR_COLOR);

      // Duration text
      textDUR = new BText(
         parent, GLOBAL.statusBG_X+GLOBAL.statusBG_W,
         GLOBAL.statusBG_Y, 53, GLOBAL.statusBG_H
      );
      textDUR.setColor(GLOBAL.text_COLOR);
      textDUR.setFont(GLOBAL.status_FONT);
      
      // Position text background
      textPOSBG = new BView(
         parent, GLOBAL.statusBG_X, GLOBAL.statusBG_Y-GLOBAL.statusBG_H,
         50, GLOBAL.statusBG_H
      );
      textPOSBG.setResource(GLOBAL.statusBG_COLOR);
      textPOSBG.setTransparency((float)0.25);

      // Position text
      textPOS = new BText(
         parent, GLOBAL.statusBG_X-20, GLOBAL.statusBG_Y-GLOBAL.statusBG_H,
         50, GLOBAL.statusBG_H
      );
      textPOS.setColor(GLOBAL.text_COLOR);
      textPOS.setFont(GLOBAL.status_FONT);      
   }
   
   public void makeVisible(Boolean state) {
	   debug.print("state=" + state);
	   main.setVisible(state);
	   bar.setVisible(state);
	   textDUR.setVisible(state);
	   textPOS.setVisible(state);
	   textPOSBG.setVisible(state);
	   visible = state;
   }
      
   public void Update(long pos, long dur) {
      //debug.print("pos=" + pos + " dur=" + dur);
      int width = 0;
      if (dur > 0) {
         width = (int)(pos * GLOBAL.statusBG_W / dur);
      }
	   bar.setBounds(
         GLOBAL.statusBG_X, GLOBAL.statusBG_Y, width, GLOBAL.statusBG_H
      );
	   pos /= 1000;
	   dur /= 1000;
	   if (dur < 216000) {
	      textDUR.setValue(String.format("|%02d:%02d",dur/60,dur%60));
	      textPOS.setValue(String.format("%02d:%02d",pos/60,pos%60));
	   } else {
         int hours = (int)dur/3600;
         int mins = (int)dur/60 - (hours*60);
         textDUR.setValue(String.format("|%02d:%02d",hours,mins));
         hours = (int)pos/3600;
         mins = (int)pos/60 - (hours*60);
         textPOS.setValue(String.format("%02d:%02d",hours,mins));
	   }
	   textPOS.setBounds(
         GLOBAL.statusBG_X+width-25, GLOBAL.statusBG_Y-GLOBAL.statusBG_H,
         50, GLOBAL.statusBG_H
       );
	   textPOSBG.setBounds(
         GLOBAL.statusBG_X+width-25, GLOBAL.statusBG_Y-GLOBAL.statusBG_H,
         50, GLOBAL.statusBG_H
      );
   }
   
   protected void remove() {
      debug.print("");
	   bar.remove();
	   main.remove();
	   textDUR.setValue(null);
	   textDUR.clearResource();
	   textDUR.remove();
	   textPOS.setValue(null);
	   textPOS.clearResource();
	   textPOS.remove();
	   textPOSBG.clearResource();
	   textPOSBG.remove();
   }
}
